let obj = {}
let watchers = []

export function queueWatcher(watcher){
    if(!obj[watcher.id]) {
        obj[watcher.id] = true
        watchers.push(watcher)
    }

    nextTick(queueWalk)  // 这里进入异步微任务里面， 就可以让所有同步代码的 dep.notify()执行完毕后，再执行此方法， 即可避免每次dep.notify()都执行一遍 更新回调（compiler）了
}

function queueWalk(){
    watchers.forEach(watcher => {
        watcher.run()
    })
    obj = {}
    watchers = []
}

let callbacks = []

function flushCallbacks(){
    callbacks.forEach(fn => fn())
    callbacks = []
}


export function nextTick(fn){
    callbacks.push(fn)
    return Promise.resolve().then(flushCallbacks)
}
